﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using System.Linq;

namespace Billing.Win
{
    public partial class ItemPricesForm : Billing.WindowsBase.BaseList
    {
        BillingDataContext mBillingDataContext = new BillingDataContext();
        public ItemPricesForm()
        {
            InitializeComponent();
        }

        protected override void onDataLoad()
        {
            base.onDataLoad();
            this.bsElement.DataSource = mBillingDataContext.ItemPrices;
            this.ElementRowTemplate = ultraGridRowEditTemplate1;
            ClientTypeComboBox.DataSource = mBillingDataContext.ClientTypes;
            TaxComboBox.DataSource = mBillingDataContext.Taxes;
        }
        protected override void onElementInitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            base.onElementInitializeLayout(sender, e);
            if (e.Layout.Bands[0].Columns.Count > 0)
            {
                e.Layout.Bands[0].Columns["ID"].Hidden = true;
                e.Layout.Bands[0].Columns["ItemID"].Hidden = true;
                e.Layout.Bands[0].Columns["ClientTypeID"].Hidden = true;
                e.Layout.Bands[0].Columns["TaxID"].Hidden = true;

                e.Layout.Bands[0].Columns["Price"].Header.Caption = "თანხა";
                e.Layout.Bands[0].Columns["ClientType"].Header.Caption = "კლიენტის ტიპი";
                e.Layout.Bands[0].Columns["Item"].Header.Caption = "სერვისი";
                e.Layout.Bands[0].Columns["Item"].Width = 200;
                e.Layout.Bands[0].Columns["Item"].Header.VisiblePosition = 0;
                e.Layout.Bands[0].Columns["Tax"].Header.Caption = "გადასახადი";
            }
        }
        protected override void onDelete()
        {
            base.onDelete();
            bsElement.RemoveCurrent();
            this.onDataLoad();
        }
        protected override void onSave()
        {
            base.onSave();
            mBillingDataContext.SubmitChanges();
        }
        protected override void onClose()
        {
            if (AllowSave && (mBillingDataContext.GetChangeSet().Deletes.Count > 0
                || mBillingDataContext.GetChangeSet().Updates.Count > 0
                || mBillingDataContext.GetChangeSet().Inserts.Count > 0))
            {
                if (MessageBox.Show("შევინახო შეცვლილი მონაცემები?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    mBillingDataContext.SubmitChanges();
            }
            base.onClose();
        }

        private void btnTemplateOk_Click(object sender, EventArgs e)
        {
            // This code was automatically generated by the RowEditTemplate Wizard
            // 
            // Close the template and save any pending changes.
            this.ultraGridRowEditTemplate1.Close(true);

        }
        private void btnTemplateCancel_Click(object sender, EventArgs e)
        {
            // This code was automatically generated by the RowEditTemplate Wizard
            // 
            // Close the template and discard any pending changes.
            this.ultraGridRowEditTemplate1.Close(false);

        }
        private void ItemButton_Click(object sender, EventArgs e)
        {
            ItemsForm form = new ItemsForm(false);
            form.AllowAdd = false;
            form.AllowEdit = false;
            form.AllowDelete = false;
            form.AllowSave = false;
            form.AllowSelect = true;
            if (form.ShowDialog() == DialogResult.OK)
            {
                //(this.ultraGridRowEditTemplate1.Row.ListObject as Billing.DataObjects.ItemPrice).Item = ((Item)form.SelectedObjects[0].CurrentObject);
                ItemsSelectResult itemsSelectResult = ((ItemsSelectResult)form.SelectedObjects[0].CurrentObject);
                IEnumerable<Item> items = from n in mBillingDataContext.Items
                                         where n.ID == itemsSelectResult.ID
                                        select n;
                foreach (Item i in items)
                {
                    (this.ultraGridRowEditTemplate1.Row.ListObject as Billing.DataObjects.ItemPrice).Item = i;
                }
            }
        }
        private void ClientTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ClientTypeComboBox.SelectedValue != null && this.ultraGridRowEditTemplate1.Row != null)
            {
                ClientType clientType = (from n in mBillingDataContext.ClientTypes
                                         where n.ID == Convert.ToInt16(ClientTypeComboBox.SelectedValue)
                                         select n).First();
                (this.ultraGridRowEditTemplate1.Row.ListObject as Billing.DataObjects.ItemPrice).ClientType = clientType;
            }
        }
        private void TaxComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TaxComboBox.SelectedValue != null && this.ultraGridRowEditTemplate1.Row != null)
            {
                Tax tax = (from n in mBillingDataContext.Taxes
                           where n.ID == Convert.ToInt16(TaxComboBox.SelectedValue)
                           select n).First();
                (this.ultraGridRowEditTemplate1.Row.ListObject as Billing.DataObjects.ItemPrice).Tax = tax;
            }
        }
    }
}
